package com.xingxue.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public abstract class DigestUtil {

	private static final char[] HEX = "0123456789abcdef".toCharArray();

	public static String toHex(byte[] digest) {
		StringBuilder sb = new StringBuilder(32);
		for (int i = 0; i < digest.length; i++) {
			byte b = digest[i];
			sb.append(HEX[b >> 4 & 0xf]);
			sb.append(HEX[b & 0xf]);
		}
		return sb.toString();
	}

	public static String createToken() {
		try {
			UUID uuid = UUID.randomUUID();
			MessageDigest md = MessageDigest.getInstance("md5");
			byte[] digest = md.digest(uuid.toString().getBytes());
			return toHex(digest);
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
	}

}
